package com.lanou3g.k12.service.impl;

import com.lanou3g.k12.entity.Course;
import com.lanou3g.k12.entity.CourseLevel;
import com.lanou3g.k12.entity.CourseType;
import com.lanou3g.k12.mapper.CourseLevelMapper;
import com.lanou3g.k12.mapper.CourseMapper;
import com.lanou3g.k12.mapper.CourseTypeMapper;
import com.lanou3g.k12.service.CourseLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseLevelServiceImpl implements CourseLevelService {

    @Autowired
    CourseLevelMapper courseLevelMapper;

    @Autowired
    CourseTypeMapper courseTypeMapper;

    @Autowired
    CourseMapper courseMapper;


    @Override
    public List<CourseLevel> findAll() {
      List<CourseLevel> courseLevels = courseLevelMapper.findAll();
        return null;
    }

    @Override
    public List<CourseType> findCoursesByLevel(Long id) {

        List<CourseType> courseTypes = courseTypeMapper.findTypesByLevel(id);

        List<CourseType> courseTypesAll = new ArrayList<>();

        for (CourseType courseType : courseTypes) {

            List<Course> courses = courseMapper.findByLevel(courseType.getId(),id);

            courseType.setCourses(courses);

            courseTypesAll.add(courseType);
        }

       return courseTypesAll;
    }

    @Override
    public List<CourseType> findByLevelForBackEnd(Long id) {

        List<CourseType> courseTypes = courseTypeMapper.findTypesByLevel(id);

        List<CourseType> courseTypesAll = new ArrayList<>();

        for (CourseType courseType : courseTypes) {

            List<Course> courses = courseMapper.findByLevelForBackEnd(courseType.getId(),id);

            courseType.setCourses(courses);

            courseTypesAll.add(courseType);
        }

        return courseTypesAll;


    }
}
